package com.googlecode.stockhamster.stockmarket;

import java.sql.*;
import java.util.ArrayList;

public class TradeItem {
	private boolean isBuy;
	private String username;
	private String moneyCode;
	private int stockID;
	private long price;
	public int amount;
	public int ID;

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public TradeItem() {
	}

	public TradeItem(boolean isBuy, String username, int stockID, long price,
			int amount, String moneyCode) {
		this.isBuy = isBuy;
		this.username = username;
		this.stockID = stockID;
		this.price = price;
		this.amount = amount;
		this.moneyCode = moneyCode;
	}

	public String getUsername() {
		return username;
	}

	public long getPrice() {
		return price;
	}

	public String getMoneyCode() {
		return moneyCode;
	}

	public void setID(int ID) {
		this.ID = ID;
	}

	public int getStockID() {
		return this.stockID;
	}

	public boolean getIsBuy() {
		return this.isBuy;
	}

	public int getID() {
		return this.ID;
	}

	public void setMoneyCode(String leftCheque) {
		this.moneyCode = leftCheque;
	}

	// for database

	public static TradeItem[] get(String username) {
		TradeItem[] targetList = new TradeItem[0];
		try {
			ArrayList<TradeItem> tempAccount = new ArrayList<TradeItem>();
			String sqlGetAccountList = "select * from StockTradeItems where username=? order by price";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			state.setString(1, username);
			rs = state.executeQuery();
			while (rs.next()) {
				TradeItem ac = new TradeItem(rs.getBoolean(1), rs.getString(2),
						rs.getInt(3), rs.getLong(4), rs.getInt(5), rs
								.getString(6));
				ac.setID(rs.getInt(7));
				tempAccount.add(ac);
			}
			targetList = new TradeItem[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i);
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return targetList;
	}

	public static TradeItem[] get(boolean isBuy, int stockID) {
		TradeItem[] targetList = new TradeItem[0];
		try {
			ArrayList<TradeItem> tempAccount = new ArrayList<TradeItem>();
			String sqlGetAccountList = "select * from StockTradeItems where isBuy=? and stockID=? order by price";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			state.setBoolean(1, isBuy);
			state.setInt(2, stockID);
			rs = state.executeQuery();
			while (rs.next()) {
				TradeItem ac = new TradeItem(rs.getBoolean(1), rs.getString(2),
						rs.getInt(3), rs.getLong(4), rs.getInt(5), rs
								.getString(6));
				ac.setID(rs.getInt(7));
				tempAccount.add(ac);
			}
			targetList = new TradeItem[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i);
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
				if (rs != null)
					rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return targetList;
	}

	public static TradeItem get(boolean isBuy, String username, int stockID,
			long price) {
		TradeItem tempAccount = new TradeItem();
		try {
			String sqlGetAccount = "select * from StockTradeItems where isBuy=? and username=? and price=? and stockID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccount);
			state.setBoolean(1, isBuy);
			state.setString(2, username);
			state.setLong(3, price);
			state.setInt(4, stockID);
			rs = state.executeQuery();
			if (!rs.next())
				return null;
			tempAccount = new TradeItem(rs.getBoolean(1), rs.getString(2), rs
					.getInt(3), rs.getLong(4), rs.getInt(5), rs.getString(6));
			tempAccount.setID(rs.getInt(7));
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
				if (rs != null)
					rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return tempAccount;
	}

	public static TradeItem[] get(boolean isBuy, String username, Integer stockID) {
		TradeItem[] targetList = new TradeItem[0];
		try {
			ArrayList<TradeItem> tempAccount = new ArrayList<TradeItem>();
			String sqlGetAccountList = "select * from StockTradeItems where isBuy=? and stockID=? and username=? order by price";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			state.setBoolean(1, isBuy);
			state.setInt(2, stockID);
			state.setString(3, username);
			rs = state.executeQuery();
			while (rs.next()) {
				TradeItem ac = new TradeItem(rs.getBoolean(1), rs.getString(2),
						rs.getInt(3), rs.getLong(4), rs.getInt(5), rs
								.getString(6));
				ac.setID(rs.getInt(7));
				tempAccount.add(ac);
			}
			targetList = new TradeItem[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i);
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
				if (rs != null)
					rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return targetList;
	}
	
	public static TradeItem get(boolean isBuy, String username, Long price) {
		TradeItem tempAccount = new TradeItem();
		try {
			String sqlGetAccount = "select * from StockTradeItems where isBuy=? and username=? and price=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccount);
			state.setBoolean(1, isBuy);
			state.setString(2, username);
			state.setLong(3, price);
			rs = state.executeQuery();
			if (!rs.next())
				return null;
			tempAccount = new TradeItem(rs.getBoolean(1), rs.getString(2), rs
					.getInt(3), rs.getLong(4), rs.getInt(5), rs.getString(6));
			tempAccount.setID(rs.getInt(7));
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
				if (rs != null)
					rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return tempAccount;
	}

	public boolean save() {
		boolean flag = false;
		try {
			String sqlSetArbiterAccount = "update StockTradeItems set isBuy=?, username=?, stockID=?, price=?, amount=?, moneyCode=? where ID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlSetArbiterAccount);
			state.setBoolean(1, this.isBuy);
			state.setString(2, this.username);
			state.setInt(3, this.stockID);
			state.setLong(4, this.price);
			state.setInt(5, this.amount);
			state.setString(6, this.moneyCode);
			state.setInt(7, this.ID);
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

	public boolean insert() {
		boolean flag = false;
		try {
			String sqlAddAmount = "insert into StockTradeItems(isBuy, username, stockID, price, amount, moneyCode) values(?,?,?,?,?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlAddAmount);
			state.setBoolean(1, isBuy);
			state.setString(2, username);
			state.setInt(3, stockID);
			state.setLong(4, price);
			state.setInt(5, amount);
			state.setString(6, moneyCode);
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}

			}
		}
		return flag;
	}

	public boolean delete() {
		boolean flag = false;
		try {
			String sqlDelAccount = "delete from StockTradeItems where ID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlDelAccount);
			state.setInt(1, this.ID);
			state.executeUpdate();
			flag = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
				}
			}
		}
		return flag;
	}

}
